/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cfs.controller;

import cfs.databean.CFSTransaction;
import cfs.databean.Customer;
import cfs.databean.Fund;
import cfs.databean.FundPriceHistory;
import cfs.formbean.TransactionForm;
import cfs.model.CustomerDAO;
import cfs.model.FundDAO;
import cfs.model.FundPriceHistoryDAO;
import cfs.model.Model;
import cfs.model.TransactionDAO;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.mybeans.dao.DAOException;

/**
 *
 * @author danny seow
 */
public class ViewCustomerTransactionAction extends Action{
    private TransactionDAO transactionDAO;
    private FundDAO fundDAO;
    private FundPriceHistoryDAO fundPriceDAO;
    private CustomerDAO customerDAO;
    
    ViewCustomerTransactionAction(Model model) {
       transactionDAO=  model.getTransactionDao();
       fundDAO = model.getFundDao();
       fundPriceDAO = model.getFundHistoryDao();
       customerDAO = model.getCustomerDao();
    }
    
    @Override
    public String getName() {
        return "customerTransaction.do";
    }

    @Override
    public String perform(HttpServletRequest request) {
        ArrayList<String> errors = new ArrayList<String>();
        try {
            Customer user = new Customer();
             if(request.getParameter("selectedUser")!=null){
                String ids= request.getParameter("selectedUser");
                int id = Integer.parseInt(ids);
                user= customerDAO.lookup(id);
             }else{
                 HttpSession session = request.getSession();
                 user = (Customer) session.getAttribute("user");
             }
             if(user == null){
                 errors.add("Un-able to retrieve user");
                 request.setAttribute("errors", errors);
                 return "ViewCustomerTransaction.jsp";
             }
             if(user !=null){
                  List<CFSTransaction> pendingListTem  = transactionDAO.getPendingTransactionListByCustomer(user.getCustomer_id(), "pending");
                  List<CFSTransaction> executedListTem  = transactionDAO.getPendingTransactionListByCustomer(user.getCustomer_id(), "Executed");
                  Collections.reverse(pendingListTem);
                  Collections.reverse(executedListTem);
                  List<TransactionForm> pendingList = new ArrayList<TransactionForm>();
                  List<TransactionForm> executedList = new ArrayList<TransactionForm>();
                  for (CFSTransaction tran : pendingListTem) {
				if (tran.getFund_id() > 0) {
					Fund fund = fundDAO.lookup(tran.getFund_id());
					FundPriceHistory fundPrice;
					TransactionForm form;
					if (tran.getExecute_date() != null) {
						fundPrice = fundPriceDAO.lookup(fund.getFund_id(),
								tran.getExecute_date());
						form = new TransactionForm(tran, fund,
									fundPrice.getPrice());
					}else{
						 form = new TransactionForm(tran, fund);
					}
					
					pendingList.add(form);
				} else {
					TransactionForm form = new TransactionForm(tran);
					pendingList.add(form);
				}
                  }
                  for (CFSTransaction tran : executedListTem) {
				if (tran.getFund_id() > 0) {
					Fund fund = fundDAO.lookup(tran.getFund_id());
					FundPriceHistory fundPrice;
					TransactionForm form;
					if (tran.getExecute_date() != null) {
						fundPrice = fundPriceDAO.lookup(fund.getFund_id(),
								tran.getExecute_date());
						form = new TransactionForm(tran, fund,
									fundPrice.getPrice());
					}else{
						 form = new TransactionForm(tran, fund);
					}
					
					executedList.add(form);
				} else {
					TransactionForm form = new TransactionForm(tran);
					executedList.add(form);
				}
                  }
                  if(pendingList == null || pendingList.size()<=0){
                      errors.add("No Pending Transaction");
                  }
                  if(executedList == null || executedList.size()<=0){
                      errors.add("No Executed Transaction");
                  }
                  
                  request.setAttribute("errors", errors);
                  request.setAttribute("pendingList", pendingList);
                  request.setAttribute("executedList", executedList);
                  
             }
             return "ViewCustomerTransaction.jsp";
        } catch (DAOException ex) {
                errors.add("Un-able to retrieve customer information");
                return "ViewCustomerTransaction.jsp";
        }
    }
    
}
